/**
 * 
 */
package com.eagle.coders.common.ui.form.renderer;

import java.util.Collection;

import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;

import com.eagle.coders.common.form.annotations.DomainDisplayPolicy;
import com.eagle.coders.common.ui.interfaces.IComponent;
import com.eagle.coders.common.ui.lookup.EagleLookup;

/**
 * @author Anees
 *
 */
public class ApplicationRenderer {
	
//	TODO: render all UI form to xml with beans xml information
	
	private Document document;
	private Element rootElement;
	
//	static{
//		document = DocumentFactory.getInstance().createDocument();
//		rootElement = document.addElement("usecases");
//	}

	public ApplicationRenderer(){
		document = DocumentFactory.getInstance().createDocument();
		rootElement = document.addElement("usecases");
		
	}
	
	/**
	 * 
	 * @return
	 */
	public String generateUsecaseForm(){
		return generateUsecaseForm(null, null);
	}
	
	/**
	 * 
	 * @param component
	 * @param isXML
	 * @return
	 */
	public String generateUsecaseForm(final IComponent component, final Boolean isXML){
		if(null == component){
			createUsecaseForms();
		}else{
			createUserForms(component, isXML);
		}
//		System.out.println("Application xml :\n"+rootElement.asXML());
		return rootElement.asXML();
	}
	
	/**
	 * 
	 */
	private void createUsecaseForms(){
		Collection<? extends IComponent> components = EagleLookup.getUsecaseLookup();
		if(null != components && components.size() != 0){
			for(IComponent component : components){
				createUserForms(component, false);
			}
		}else{
			IComponent component = EagleLookup.getUsecaseFromXml();
			createUserForms(component,true);
		}
	}
	
	/**
	 * 
	 * @param component
	 * @param isXML
	 */
	private void createUserForms(final IComponent component, final Boolean isXML){
		FormRenderer formRenderer = new FormRenderer();
		formRenderer.render(component, rootElement, isXML);
//		FormRenderer.getInstance().render(component, rootElement, isXML);
	}
	
//	TODO: render all Supporting  widgets for the Usecase

}
